package com.book.Dao;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jdbc.dao.SelectUser;
import com.jdbc.tool.DBLianjie;

public class Addorders extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//获得time
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		             String time=sdf.format(new Date());
		       //获得号码
		    CommUtilsTest commUtilsTest=new CommUtilsTest();
		    String xString=commUtilsTest.OrderNumber();
		    //bookid
		    String bookid=null;
            //获得数量
		    String numbersString=request.getParameter("numbers");
		    String bookprice=request.getParameter("bookprice");
		    int numbers=Integer.parseInt(numbersString);
		    int price=Integer.parseInt(bookprice);
		    //算出合计；
		    int heji=numbers*price;

		    
            //获得地址
		    String address=request.getParameter("address");
            if(request.getParameter("bookid")!=null){
            	bookid = new String(request.getParameter("bookid").getBytes("iso8859-1"),"utf-8"); 
            }
            int bookid1=Integer.parseInt(bookid);
            //获得e_user_id;
            String e_user_id=request.getParameter("e_user_id");
            //插入数据库中。
            Connection connection=null;
            Statement statement=null;
            connection=DBLianjie.getconn();
            String sql="insert into orders (ORDERID,TIMES,ADDRESS,NUMBERS,STATUS,BOOKID,E_USER_ID) values ('"+xString+"',to_date('"+time+"','yyyy-MM-dd'),'"+address+"',"+numbers+",1,"+bookid1+",'"+e_user_id+"')";
            try {
				statement=connection.createStatement();
				boolean x=statement.execute(sql);
				if(!x){
					response.sendRedirect("./qiantai/ordersucc.jsp?");
				    HttpSession session=request.getSession();
				    session.setAttribute("xString",xString);
				    session.setAttribute("heji",heji);
				    session.setAttribute("address",address);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
            
	}

}
